unit FRPreviewFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, xmldom, XMLIntf, msxmldom, XMLDoc, frxExportPDF, frxExportXLS,
  frxGradient, frxChBox, frxCross, frxRich, frxOLE, frxBarcode, ImgList,
  frxClass, frxChart, Menus, frxExportRTF, frxExportXML, frxExportHTML,
  frxExportTXT, frxExportImage, DB, ADODB, frxPreview, StdCtrls, Mask,
  frxCtrls, ComCtrls, ToolWin, frxExportCSV;

type
  TFRPreviewForm = class(TForm)
    ToolBar1: TToolBar;
    btnPrint: TToolButton;
    ToolButton4: TToolButton;
    ZoomWholePageB: TToolButton;
    ZoomPageWidthB: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ZoomCB: TfrxComboBox;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    mskPreviewPage: TMaskEdit;
    ToolButton9: TToolButton;
    ToolButton10: TToolButton;
    ToolButton12: TToolButton;
    btnExport: TToolButton;
    ToolButton1: TToolButton;
    frPreview1: TfrxPreview;
    dsQuery: TADODataSet;
    SaveDialog1: TSaveDialog;
    ExportPopup: TPopupMenu;
    SaveasPDFfileP1: TMenuItem;
    SaveasExcelFileE1: TMenuItem;
    SaveasXMLFileX1: TMenuItem;
    frxChartObject1: TfrxChartObject;
    frReport1: TfrxReport;
    ImageList1: TImageList;
    frxBarCodeObject1: TfrxBarCodeObject;
    frxOLEObject1: TfrxOLEObject;
    frxRichObject1: TfrxRichObject;
    frxCrossObject1: TfrxCrossObject;
    frxCheckBoxObject1: TfrxCheckBoxObject;
    frxGradientObject1: TfrxGradientObject;
    XMLDocument1: TXMLDocument;
    frxPDFExport1: TfrxPDFExport;
    frxXLSExport1: TfrxXLSExport;
    frxCSVExport1: TfrxCSVExport;
    procedure ZoomWholePageBClick(Sender: TObject);
    procedure ZoomPageWidthBClick(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure ToolButton10Click(Sender: TObject);
    procedure SaveasPDFfileP1Click(Sender: TObject);
    procedure SaveasExcelFileE1Click(Sender: TObject);
    procedure SaveasXMLFileX1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure btnPrintClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ZoomCBChange(Sender: TObject);
  private
    { Private declarations }
    FPageCount: Integer;
  public
    { Public declarations }
    procedure UpdateZoom;
  end;

var
  FRPreviewForm: TFRPreviewForm;

implementation

{$R *.dfm}

procedure TFRPreviewForm.UpdateZoom;
begin
  ZoomCB.Text := IntToStr(Round(frPreview1.Zoom * 100)) + '%';
end;

procedure TFRPreviewForm.ZoomWholePageBClick(Sender: TObject);
begin
  frPreview1.ZoomMode := zmWholePage;
  UpdateZoom;
end;

procedure TFRPreviewForm.ZoomPageWidthBClick(Sender: TObject);
begin
  frPreview1.ZoomMode := zmPageWidth;
  UpdateZoom;
end;

procedure TFRPreviewForm.ToolButton5Click(Sender: TObject);
begin
  frPreview1.Zoom := 1;
  UpdateZoom;
end;

procedure TFRPreviewForm.ToolButton7Click(Sender: TObject);
begin
   frPreview1.First;
  mskPreviewPage.Text := '1';
end;

procedure TFRPreviewForm.ToolButton8Click(Sender: TObject);
begin
  frPreview1.Prior;
  FPageCount := StrToInt(mskPreviewPage.Text) - 1;
  if FPageCount >= 1 then
     mskPreviewPage.Text := IntToStr(FPageCount);
end;

procedure TFRPreviewForm.ToolButton9Click(Sender: TObject);
begin
  frPreview1.Next;
  FPageCount := StrToInt(mskPreviewPage.Text) + 1;
  if FPageCount <= frPreview1.PageCount then
     mskPreviewPage.Text := IntToStr(FPageCount);
end;

procedure TFRPreviewForm.ToolButton10Click(Sender: TObject);
begin
  frPreview1.Last;
  FPageCount := frPreview1.PageCount;
  mskPreviewPage.Text := IntToStr(FPageCount);
end;

procedure TFRPreviewForm.SaveasPDFfileP1Click(Sender: TObject);
begin
   frPreview1.Export(frxPDFExport1);
end;

procedure TFRPreviewForm.SaveasExcelFileE1Click(Sender: TObject);
begin
  frPreview1.Export(frxXLSExport1);
end;

procedure TFRPreviewForm.SaveasXMLFileX1Click(Sender: TObject);
begin
  frPreview1.Export(frxCSVExport1)
end;

procedure TFRPreviewForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := CaFree;
end;

procedure TFRPreviewForm.FormCreate(Sender: TObject);
begin
  ZoomCB.Items.Clear;
  ZoomCB.Items.Add('90%');
  ZoomCB.Items.Add('100%');
  ZoomCB.Items.Add('125%');
  ZoomCB.Items.Add('150%');
  ZoomCB.Items.Add('200%');
end;

procedure TFRPreviewForm.btnPrintClick(Sender: TObject);
begin
  frPreview1.Print;  
end;

procedure TFRPreviewForm.ToolButton1Click(Sender: TObject);
begin
  Close;
end;

procedure TFRPreviewForm.ZoomCBChange(Sender: TObject);
var
  s: String;
begin
   //frPreview1.SetFocus;
   s := ZoomCB.Text;

   if Pos('%', s) <> 0 then
      s[Pos('%', s)] := ' ';
    while Pos(' ', s) <> 0 do
      Delete(s, Pos(' ', s), 1);

   if s <> '' then
      frPreview1.Zoom := StrToFloat(s) / 100;

   PostMessage(Handle, WM_UPDATEZOOM, 0, 0);
end;

end.
